<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% account ||= nil; current_depth ||= 0; depth ||= 1; max_sub_accounts ||= 100; sub_account_count ||= account.try(:sub_account_count); course_count ||= account.try(:course_count) %>
<% keep_going = current_depth < depth %>
<div id="account_<%= account ? account.id : "blank" %>" class="account" style="<%= hidden unless account %>">
  <div class="header">
    <div class="show_account">
      <span class="id" style="display: none;"><%= account ? account.id : nbsp %></span>
      <a href="<%= account_url(account ? account.id : "{{ id }}") %>" class="name"><%= account.name rescue nbsp %></a>
      <span class="links">
        <button
          data-link="<%= context_url(@context, :context_sub_account_url, (account ? account.id : "{{ id }}")) %>"
          class="Button Button--icon-action expand_sub_accounts_link no-hover"
          title="{{#t}}Show Sub-Accounts{{/t}}"
          style="<%= hidden if (keep_going && account && sub_account_count < max_sub_accounts) || (account && sub_account_count == 0) %>"
        >
          <i aria-hidden="true" class="icon-arrow-down standalone-icon"></i>
          <span class="screenreader-only"><%= t('Show Sub-Accounts for %{account_name}', :account_name => account&.name) %></span>
        </button>
        <button
          class="Button Button--icon-action collapse_sub_accounts_link no-hover"
          title="{{#t}}Hide Sub-Accounts{{/t}}"
          style="<%= hidden unless keep_going && account && sub_account_count < max_sub_accounts %>"
        >
          <i aria-hidden="true" class="icon-arrow-up standalone-icon"></i>
          <span class="screenreader-only"><%= t('Hide Sub-Accounts for %{account_name}', :account_name => account&.name) %></span>
        </button>
        <button
          class="Button Button--icon-action add_sub_account_link no-hover"
          title="{{#t}}Add Sub-Account{{/t}}"
          style="<%= hidden unless (keep_going && account && sub_account_count < max_sub_accounts) || (account && sub_account_count == 0) %>"
        >
          <i aria-hidden="true" class="icon-add standalone-icon"></i>
          <span class="screenreader-only"><%= t('Add Sub-Account to %{account_name}', :account_name => account&.name) %></span>
        </button>
        <button
          class="Button Button--icon-action edit_account_link no-hover"
          title="{{#t}}Edit Account Name{{/t}}"
        >
          <i aria-hidden="true" class="icon-edit standalone-icon"></i>
          <span class="screenreader-only"><%= t('Edit Account Name for %{account_name}', :account_name => account&.name) %></span>
        </button>
        <% if account && !course_count.zero? %>
          <button
            aria-disabled=true
            class="Button Button--icon-action cant_delete_account_link no-hover"
            title="{{#t}}You can't delete a sub-account that has courses in it{{/t}}"
          >
            <i aria-hidden="true" class="icon-trash standalone-icon"></i>
            <span class="screenreader-only"><%= t("You can't delete %{account_name} because it has courses in it", :account_name => account&.name) %></span>
          </button>
        <% elsif account != @context %>
          <button
            class="Button Button--icon-action delete_account_link no-hover"
            title="{{#t}}Delete Sub-Account{{/t}}"
          >
            <i aria-hidden="true" class="icon-trash standalone-icon"></i>
            <span class="screenreader-only"><%= t('Delete %{account_name}', :account_name => account&.name) %></span>
          </button>
        <% end %>
      </span>
      <div style="<%= hidden unless account && course_count > 0 %>" class="courses_count">
        <%= t('account_courses', 'Course', :count => course_count) if account %>
      </div>
      <div style="<%= hidden unless account && sub_account_count > 0 %>" class="sub_accounts_count">
        <%= t('account_sub_accounts', "Sub-Account", :count => sub_account_count) if account %>
      </div>
      <div class="clear"></div>
    </div>
    <div class="edit_account">
      <%= form_for (account || Account.new), :url => (account ? context_url(@context, :context_sub_account_url, account.id) : context_url(@context, :context_sub_accounts_url)), :html => {:class => "edit_sub_account_form form-inline", :method => (account ? :put : :post)} do |f| %>
        <%= f.text_field :name, :class => "account_name" %>
      <% end %>
    </div>
  </div>
  <ul class="sub_accounts">
    <% if account && keep_going && sub_account_count < max_sub_accounts %>
      <% @accounts[account.id][:sub_account_ids].each do |sub_account_id| %>
        <li class="sub_account">
          <% account_data = @accounts[sub_account_id] %>
          {{> views/sub_accounts}}
        </li>
      <% end %>
    <% end %>
  </ul>
</div>
